package approve.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import approve.action.Library_All_Label;
import approve.action.R;
import approve.model.ListLabel;

public class Library_Listview_Adapter extends BaseAdapter{

	Library_All_Label context;
	ArrayList<ListLabel> lstlabel = new ArrayList<ListLabel>();

	public Library_Listview_Adapter(Library_All_Label context, ArrayList<ListLabel> lst){
		this.context=context;
		this.lstlabel=lst;
	}
	ViewHolder holder;
	@Override
	public int getCount() {
		return lstlabel.size();
	}

	@Override
	public Object getItem(int arg0) {
		return lstlabel.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ListLabel onelabel = lstlabel.get(position);
		if(convertView==null){
			holder = new ViewHolder();

			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=inflater.inflate(R.layout.library_adapter_listlabel, parent,false);
			holder.lila_adapter_listlabel=(LinearLayout)convertView.findViewById(R.id.lila_adapter_listlabel);
			holder.imageView=(ImageView)convertView.findViewById(R.id.imageView1);
			holder.libraryedit=(ImageView)convertView.findViewById(R.id.libraryedit);
			holder.tvlabelname=(TextView)convertView.findViewById(R.id.tvlabelname);
			convertView.setBackgroundResource(R.drawable.list_selector);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		holder.tvlabelname.setText(onelabel.getName_lable());
		
		holder.libraryedit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((Library_All_Label)context).longClickEditLabel(onelabel,position);
			}
		});
		
		holder.lila_adapter_listlabel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				((Library_All_Label)context).getAlertwaiting2().show();

				try{
					new Thread(){
						public void run() {
							((Library_All_Label)context).processItemClickListerner(onelabel);
							((Library_All_Label)context).getUiHandler2().post( new Runnable(){
								@Override
								public void run() {
									if(((Library_All_Label)context).isUpdateRequired2()){
									}else{
										((Library_All_Label)context).getAlertwaiting2().cancel();
									}
								}
							} );
						}
					}.start();
				}catch (Exception e) {}
			}
		});
		
		return convertView;
	}

	public static class ViewHolder{
		LinearLayout lila_adapter_listlabel;
		ImageView imageView,libraryedit;
		TextView tvlabelname;
	}
}
